
-- by Chris Mackey

--pieces
local base = piece "base"
local head = piece "head"
local torso = piece "torso"
local tail_up = piece "tail_up"
local tail_dn = piece "tail_dn"

local lf_leg = piece "lf_leg"
local lf_shin = piece "lf_shin"
local lf_foot = piece "lf_foot"

local lb_leg = piece "lb_leg"
local lb_shin = piece "lb_shin"
local lb_foot = piece "lb_foot"

local rf_leg = piece "rf_leg"
local rf_shin = piece "rf_shin"
local rf_foot = piece "rf_foot"

local rb_leg = piece "rb_leg"
local rb_shin = piece "rb_shin"
local rb_foot = piece "rb_foot"

--signals
local walk = 2
local aim  = 4

local function Attack()
	Move( base, z_axis, 15, 50 )
end

local function Ready()
	Move( base, z_axis, 0, 10 )
end

local function Walk()
	SetSignalMask( walk )
	local speed1 = 3
	while( true ) do 
		Turn( base, z_axis, -.2, speed1/2 )
		Turn( head, z_axis, .2, speed1/2 )
		
		Turn( lf_leg, x_axis, -.5, speed1 )
		Turn( lf_foot, x_axis, .5, speed1 )
		
		Turn( lb_leg, x_axis, .5, speed1 )
		
		Turn( rf_leg, x_axis, .5, speed1 )
		Turn( rf_foot, x_axis, -.5, speed1 )
		
		Turn( rb_leg, x_axis, -.5, speed1 )
		Turn( rb_foot, x_axis, .5, speed1 )
		
		WaitForTurn( lf_leg, x_axis )
		Sleep( 50 )
		
		Turn( base, z_axis, .2, speed1/2 )
		Turn( head, z_axis, -.2, speed1/2 )
		
		Turn( lf_leg, x_axis, .5, speed1 )
		Turn( lf_foot, x_axis, -.5, speed1 )
		
		Turn( lb_leg, x_axis, -.5, speed1 )
		Turn( lb_foot, x_axis, .5, speed1 )
		
		Turn( rf_leg, x_axis, -.5, speed1 )
		Turn( rf_foot, x_axis, .5, speed1 )
		
		Turn( rb_leg, x_axis, .5, speed1 )
		
		WaitForTurn( lf_leg, x_axis )
		Sleep( 50 )
	end
end

function script.StartMoving()
	SetSignalMask( walk )
	--StartThread( StopWalk )
	StartThread( Walk )
end

function script.StopMoving()
	Signal( walk )
	SetSignalMask( walk )
	--StartThread( StopWalk )
end

function script.QueryWeapon1() return head end

function script.AimFromWeapon1() return head end

function script.AimWeapon1( heading, pitch )
	WaitForMove( base, z_axis )
	Signal( aim )
	SetSignalMask( aim )
	StartThread( Ready )
	WaitForMove( base, z_axis )
	return true
end

function script.FireWeapon1()
	StartThread( Attack )
end

function script.Killed( damage, health )
	local severity = damage/health * 100
	
	if( severity <= 50 ) then
		EmitSfx( head, 1024+1 )
		EmitSfx( torso, 1024+1 )
		return 0 -- corpsetype
	elseif( severity <= 100 ) then
		EmitSfx( head,  1024+1 )
		EmitSfx( torso, 1024+1 )
		Explode( head, SFX.FALL )
		return 0 -- corpsetype
	else
		EmitSfx( torso, 1024+2 )
		Explode( head, SFX.FALL )
		Explode( torso, SFX.FALL )
		Explode( lf_leg, SFX.FALL )
		Explode( lf_shin, SFX.FALL )
		Explode( lb_leg, SFX.FALL )
		Explode( lb_shin, SFX.FALL )
		Explode( rf_leg, SFX.FALL )
		Explode( rf_shin, SFX.FALL )
		Explode( rb_leg, SFX.FALL )
		Explode( rb_shin, SFX.FALL )
		return 1 -- corpsetype
	end
end
